package org.redmagic.domain;

import java.io.IOException;
import java.util.List;

import org.redmagic.domain.UiPropertyResponse.Deserializer;
import org.redmagic.domain.UiPropertyResponse.Serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using=Serializer.class)
@JsonDeserialize(using=Deserializer.class)
public class UiPropertyResponse {

	private final List<InternalServiceProperty> uiProperties;
	
	public UiPropertyResponse(List<InternalServiceProperty> magicUiProperties) {
		this.uiProperties = magicUiProperties;
	}

	public List<InternalServiceProperty> getMagicUiProperties() {
		return uiProperties;
	}
	
	protected static class Serializer extends JsonSerializer<UiPropertyResponse> {
		@Override
		public void serialize(UiPropertyResponse value, JsonGenerator jgen,
				SerializerProvider provider) throws IOException,
				JsonProcessingException {
			
			jgen.writeStartArray();
			for (InternalServiceProperty magicUiProperty : value.getMagicUiProperties()) {
				jgen.writeObject(magicUiProperty);
			}
			jgen.writeEndArray();
		}
	}
	
	protected static class Deserializer extends JsonDeserializer<UiPropertyResponse> {
		@Override
		public UiPropertyResponse deserialize(JsonParser jp,
				DeserializationContext ctxt) throws IOException,
				JsonProcessingException {
			
			// TODO clean this up
//			@SuppressWarnings("unchecked")
//			List<List<String>> hierarchy = jp.readValueAs(List.class);
//			
//			Hierarchy.Builder builder = new Hierarchy.Builder();
//			for (List<String> tags : hierarchy) {
//				builder.withTags(new HashSet<String>(tags));
//			}
//			
//			return builder.build();
			return null;
		}
	}
}
